<?php
/**
 * Created by PhpStorm.
 * User: hjc_0
 * Date: 2016/5/9
 * Time: 10:00
 */
class AccessControlService extends BaseService
{
    private $reqUrl;

    private $accessControlConfDao;      //门禁配置DAO
    private $accessControlDao;          //门禁信息DAO
    private $accessControlLogDao;       //门禁日志DAO


    const ACCESS_CONTROL_EMPTY = 1000;  //门禁编号为空
    const COMMUNITY_EMPTY = 1001;       //园区为空
    const BUILD_EMPTY = 1002;           //楼幢为空
    const OPEN_DOOR_MORE = 1003;        //楼幢为空
    const LINK_REMOTE_FAILED = 1100;    //远程连接失败
    const REQUEST_SUCCESS = 200;        //开门成功
    const REQUEST_FAILED = 9999;        //开门失败


    public function _initialize()
    {
        parent::_initialize();

        import('@.ORG.Http');   //加载网络请求类

//        $this->reqUrl = 'http://accesscontrol.golive.cc/api.php';
        $this->reqUrl = 'http://183.129.235.99/api.php';

        $this->accessControlConfDao = D('AccessControlConf');
        $this->accessControlDao = D('AccessControl');
        $this->accessControlLogDao = D('AccessControlLog');
    }

    /**
     * 远程开门操作
     * @param int $accountId 企业账号信息
     * @param int $commId 园区id
     * @param int $buildId 楼幢id
     * @return
     */
    public function remoteOpenDoor($accountId, $commId, $buildId)
    {
        //数据验证
//        if(!$commId) {
//            return array('err_code' => self::COMMUNITY_EMPTY,'err_msg' => '园区不能为空');
//        }
//        if(!$buildId) {
//            return array('err_code' => self::BUILD_EMPTY,'err_msg' => '楼幢不能为空');
//        }


        //获得门禁信息
        $gatWayInfo = $this->getGatWayInfo($accountId, $commId, $buildId);
        if (!$gatWayInfo) {
            return array('err_code' => self::ACCESS_CONTROL_EMPTY, 'err_msg' => '门禁设备信息未注册');
        }

        //判断是否开过
        $userId = session('userId');
        $cacheKey = "accessControl_{$accountId}_{$userId}";
        $res = RedisService::get($cacheKey);
        if ($res) {
            return array('err_code' => self::OPEN_DOOR_MORE, 'err_msg' => '不能频繁操作开门');
        }

        //请求数据操作
        $postData = array('gateway_index' => $gatWayInfo['gateway_index']);
//        $headerData = array("content-type: application/x-www-form-urlencoded;charset=UTF-8");
        $data = Http::curlPost($this->reqUrl, $postData);

        if (!$data) {
            return array('err_code' => self::LINK_REMOTE_FAILED, 'err_msg' => '远程连接失败！');
        }

        //获得请求数据操作
        $res = json_decode($data, true);
        if ($res['err_code'] == 200) {
            $cacheData = array('op_time' => time());
            RedisService::set($cacheKey, $cacheData, 90);

            $this->addOpenDoorLog($gatWayInfo['control_id']);
            return array('err_code' => self::REQUEST_SUCCESS, 'err_msg' => '开门成功');
        } else {
            return array('err_code' => self::REQUEST_FAILED, 'err_msg' => '开门失败！');
        }
    }

    /**
     * 添加开门记录
     * @param int $controlId 门禁id
     * @param int $userId 用户id
     * @param int $wechatUserId 微信用户id
     */
    public function addOpenDoorLog($controlId)
    {
        $data = array(
            'user_id' => session('userId'),
            'wechat_user_id' => session('wechatUserId'),
            'control_id' => $controlId,
            'account_id' => session('accountId'),
            'operation_time' => time()
        );

        $res = $this->accessControlLogDao->add($data);
        return $res;
    }

    /**
     * 获得开门的设备信息
     * @param int $ccountId 账号id
     * @param int $commId 园区id
     * @param int $buildId 楼幢id
     * @return array
     */
    public function getGatWayInfo($accountId, $commId, $buildId)
    {
        $condition = array(
            'account_id' => $accountId,
//            'community_id' => $commId,
//            'building_id' => $buildId
        );
        $data = $this->accessControlDao->getInfo($condition, '');

        return $data;
    }

    /**
     * 获得门禁配置信息
     * @param int $confId 门禁配置id
     */
    public function getAccessControlConf($confId)
    {
        if (!$confId) {
            return false;
        }

        $condition = array(
            'conf_id' => $confId
        );
        $data = $this->accessControlConfDao->getInfo($condition, '');

        return $data;
    }

    /**
     * 获得用户信息
     * @return bool|mixed
     */
    public function getUserInfo($userId)
    {
        if (!$userId) {
            return false;
        }

        //获得用户信息
        $userData = D('User')->getInfo(array('user_id' => $userId));

        //获得用户信息
        $userInfoData = D('UserInfo')->getInfo(array('user_id' => $userData['user_id']), 'user_id,room_id');
//        if (empty($userInfoData['user_id'])) {
//            return false;
//        }

        //获得楼幢信息
        $buildId = D('Room')->getFieldByCondition(array('room_id' => $userInfoData['room_id']), 'building_id');
//        if (!$buildId) {
//            return false;
//        }

        //获得园区信息
        $commId = D('Building')->getFieldByCondition(array('building_id' => $buildId), 'community_id');
//        if (!$commId) {
//            return false;
//        }

        $userData['build_id'] = $buildId;
        $userData['comm_id'] = $commId;

        return $userData;
    }

    /**
     * 获得门禁授权信息,针对老师和宿管员
     * @param array $userInfo
     */
    public function getAccessControlAuthInfo($userInfo)
    {

    }
}